<?php

namespace app\admin\controller;

use app\admin\service\ArticleService;
use support\spark\model\system\Article;
use support\spark\model\system\ArticleCate;
use support\Request;
use support\View;

class ArticleController extends Curd
{

    /**
     * 获取列表
     */
    public function index()
    {
        if (request()->isAjax()) {

            $articleService = new ArticleService();
            $res = $articleService->getList(request()->all());
            return sparkSuccess($res);
        }
        return view('article/index');
    }

    /**
     * 添加
     */
    public function add()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            $articleService = new ArticleService();
            $res = $articleService->addArticle($param);
            return sparkSuccess($res);
        }

        $articleCateModel = new ArticleCate();
        View::assign([
            'cate' => json_encode($articleCateModel->getAllList([
                'status' => 1
            ], ['id', 'name']))
        ]);

        return view('article/add');
    }

    /**
     * 编辑
     */
    public function edit()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();

            $articleService = new ArticleService();
            $res = $articleService->editArticle($param);
            return sparkSuccess($res);
        }

        $articleCateModel = new ArticleCate();
        $id = request()->input('id');
        $articleModel = new Article();
        View::assign([
            'info' => json_encode($articleModel->getInfoById($id)),
            'cate' => json_encode($articleCateModel->getAllList([
                'status' => 1
            ], ['id', 'name']))
        ]);

        return view('article/edit');
    }

    /**
     * 删除
     */
    public function del()
    {
        $id = request()->input('id');

        $articleModel = new Article();
        $articleModel->delById($id);
        return sparkSuccess(dataReturn(0, '删除成功'));
    }
}